﻿using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia;
using JGSY.AI.DesktopUI.Services;

namespace JGSY.AI.DesktopUI.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    public string Greeting { get; } = "欢迎使用 JGSY.AI 智能应用!";

    [ObservableProperty]
    private string _currentTheme = "Ocean"; // 默认设置为海洋蓝

    [ObservableProperty]
    private bool _isSettingsOpen = false;

    [ObservableProperty]
    private SettingsViewModel? _settingsViewModel;

    private readonly IThemeService _themeService;

    public ICommand SettingsCommand { get; }
    public ICommand ProfileCommand { get; }
    public ICommand HideCommand { get; }
    public ICommand MaximizeCommand { get; }
    public ICommand CloseCommand { get; }
    public ICommand ToggleThemeCommand { get; }

    public MainWindowViewModel()
    {
        _themeService = App.ThemeService;
        CurrentTheme = _themeService.CurrentTheme;
        
        SettingsCommand = new RelayCommand(OnSettings);
        ProfileCommand = new RelayCommand(OnProfile);
        HideCommand = new RelayCommand(OnHide);
        MaximizeCommand = new RelayCommand(OnMaximize);
        CloseCommand = new RelayCommand(OnClose);
        ToggleThemeCommand = new RelayCommand(OnToggleTheme);
        
        // 订阅主题变更事件
        _themeService.ThemeChanged += OnThemeChanged;
    }

    private void OnSettings()
    {
        // 创建或重用设置视图模型
        if (SettingsViewModel == null)
        {
            SettingsViewModel = new SettingsViewModel();
            SettingsViewModel.CloseRequested += (s, e) => IsSettingsOpen = false;
        }
        
        // 切换设置抽屉的显示状态
        IsSettingsOpen = !IsSettingsOpen;
    }

    private void OnProfile()
    {
        // Open profile window
        var profileWindow = new JGSY.AI.DesktopUI.Views.ProfileWindow
        {
            DataContext = new ProfileViewModel()
        };
        
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop 
            && desktop.MainWindow != null)
        {
            profileWindow.ShowDialog(desktop.MainWindow);
        }
        else
        {
            profileWindow.Show();
        }
    }

    private void OnHide()
    {
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var window = desktop.MainWindow;
            if (window != null)
            {
                window.WindowState = WindowState.Minimized;
            }
        }
    }

    private void OnMaximize()
    {
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var window = desktop.MainWindow;
            if (window != null)
            {
                window.WindowState = window.WindowState == WindowState.Maximized 
                    ? WindowState.Normal 
                    : WindowState.Maximized;
            }
        }
    }

    private void OnClose()
    {
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.Shutdown();
        }
    }

    private void OnToggleTheme()
    {
        _themeService.SwitchToNextTheme();
    }

    private void OnThemeChanged(object? sender, string themeName)
    {
        CurrentTheme = themeName;
    }
}
